package com.lujianfei.powersearch.ui.preview.adapter

import android.view.View
import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableArrayList
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.lujianfei.powersearch.R
import com.lujianfei.powersearch.base.BaseDatabindingAdapter
import com.lujianfei.powersearch.databinding.AdapterImageItemBinding
import com.lujianfei.powersearch.utils.ImageUtils

class ImagePreviewAdapter(items: ObservableArrayList<String>) :
    BaseDatabindingAdapter<String, AdapterImageItemBinding>(items) {

    var onViewTapListener: ((View) -> Unit)? = null
    var onLongClickListener: ((String) -> Unit)? = null

    override fun getLayoutResId() = R.layout.adapter_image_item

    override fun onBindItem(binding: AdapterImageItemBinding?, bean: String, position: Int) {
        binding?.photoView?.setOnViewTapListener { view, x, y ->
            onViewTapListener?.invoke(view)
        }
        binding?.photoView?.setOnLongClickListener {
            onLongClickListener?.invoke(bean)
            true
        }
        binding?.showPlay = bean.contains("video")
        binding?.photo = ImageUtils.getImageByPath(bean)
    }
}

@BindingAdapter(
    value = ["bindingImagePreview", "currentIndex", "onPageSelected", "onViewTapListener", "onLongClickListener"],
    requireAll = false
)
fun bindingImagePreview(
    viewPager2: ViewPager2,
    imageList: ObservableArrayList<String>,
    currentIndex:Int = 0,
    onPageSelected: ((position: Int, realPosition:Int) -> Unit)? = null,
    onViewTapListener:((View)->Unit) ?= null,
    onLongClickListener:((String)->Unit) ?= null
) {
    if (viewPager2.adapter != null) {
        viewPager2.setCurrentItem(currentIndex, false)
        return
    }
    viewPager2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            if (imageList.size > 1) {
                if (position == 0) {
                    onPageSelected?.invoke(imageList.size - 3, position)
                } else if (position == imageList.size - 1){
                    onPageSelected?.invoke(0, position)
                } else {
                    onPageSelected?.invoke(position - 1, position)
                }
            } else {
                onPageSelected?.invoke(position, position)
            }
        }

        override fun onPageScrollStateChanged(state: Int) {
            super.onPageScrollStateChanged(state)
            if (imageList.size > 1) {
                val fakeSize = imageList.size
                val currentPosition = viewPager2.currentItem
                if (state == ViewPager2.SCROLL_STATE_IDLE) {
                    if (viewPager2.currentItem == 0) {
                        viewPager2.setCurrentItem(fakeSize - 2, false);
                    } else if (currentPosition == fakeSize - 1) {
                        viewPager2.setCurrentItem(1, false);
                    }
                } else if (state == ViewPager2.SCROLL_STATE_DRAGGING && currentPosition == fakeSize) {
                    //we scroll too fast and miss the state SCROLL_STATE_IDLE for the previous item
                    viewPager2.setCurrentItem(2, false);
                }
            }
        }
    })
    val adapter = ImagePreviewAdapter(imageList)
    adapter.onViewTapListener = {
        onViewTapListener?.invoke(it)
    }
    adapter.onLongClickListener = {
        onLongClickListener?.invoke(it)
    }
    viewPager2.adapter = adapter
    viewPager2.setCurrentItem(currentIndex, false)
}